package com.kzd.btgame.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.FragmentHomeRankingBinding
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.jetpack.vm.GameViewModel
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class HomeRankingFragment : OwnerViewBindingFragment<FragmentHomeRankingBinding>() {

    companion object {
        fun newInstance(): HomeRankingFragment {
            return HomeRankingFragment()
        }
    }

    private val mGame1Adapter: Game1Adapter<GameBean> by lazy {
        Game1Adapter(Game1Adapter.Type.GAME_LIST_RANKING_1)
    }

    private val mViewModel: GameViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentHomeRankingBinding
        get() = FragmentHomeRankingBinding::inflate

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGame1Adapter

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            val type = when (mViewBinding.rgHomeRankingTabs.checkedRadioButtonId) {
                R.id.rgHomeRankingNew -> 1
                R.id.rgHomeRankingOnline -> 2
                else -> 0
            }
            mViewModel.gameRanking(type.toString())
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            val type = when (mViewBinding.rgHomeRankingTabs.checkedRadioButtonId) {
                R.id.rgHomeRankingNew -> 1
                R.id.rgHomeRankingOnline -> 2
                else -> 0
            }
            mViewModel.gameRanking(type.toString(), isLoadMore = true)
        }

        mViewBinding.rgHomeRankingTabs.apply {
            check(R.id.rgHomeRankingHot)
        }.setOnCheckedChangeListener { _, i ->
            val type = when (i) {
                R.id.rgHomeRankingNew -> 1
                R.id.rgHomeRankingOnline -> 2
                else -> 0
            }
            mViewModel.gameRanking(type.toString())
        }
    }

    override fun initData() {
        mViewModel.gameRanking(type = "0")
    }

    override fun initViewMode() {

        mViewModel.gameRankingLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,
                mGame1Adapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            val data = response.data?.data
                ?: return@observe
            if (isLoadMore) {
                mGame1Adapter.addData(data)
                return@observe
            }
            mGame1Adapter.setNewInstance(data)
        })
    }

}